package com.eagle.view.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;

import com.eagle.EApplication;
import com.eagle.R;
import com.eagle.service.TrafficService;
import com.eagle.service.TrafficService_;
import com.eagle.support.constants.Constants;
import com.eagle.support.utils.Utils;
import com.eagle.view.activity.base.BaseCallActivity;
import com.eagle.view.widget.base.ETextView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.OnActivityResult;
import org.androidannotations.annotations.ViewById;

/**
 * 首页
 */
@EActivity(R.layout.activity_main)
public class MainActivity extends BaseCallActivity {

    static final int LOGIN_REQUEST = 11;

    @ViewById(R.id.tv_name)
    protected ETextView mTvName;

    @Extra(Constants.CALL_TYPE)
    protected int mCallType = -1;

    @Extra(Constants.CALL_UID)
    protected String mCallUid;

    @Extra(Constants.ROOM_ID)
    protected String mRoomId;

    @Extra(Constants.MEETING_SENDER)
    protected String mMeetingSender;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setDarkStatusIcon(true);
        initExplodeTransition();
    }

    @AfterViews
    protected void handlerPage() {
        if (!mAccountHelper.isLogged()) {
            LoginActivity_.intent(this).startForResult(LOGIN_REQUEST);
        }

        if (mCallType == Constants.CALL_TYPE_INCOMING) {
            mCallType = -1;
            CallDialingActivity_.intent(this).
                    extra(Constants.CALL_TYPE, Constants.CALL_TYPE_INCOMING).
                    extra(Constants.CALL_UID, mCallUid).start();
        } else if (mCallType == Constants.CALL_TYPE_MEETING_INVITE) {
            mCallType = -1;
            CallDialingActivity_.intent(this).
                    extra(Constants.CALL_TYPE, Constants.CALL_TYPE_MEETING_INVITE).
                    extra(Constants.ROOM_ID, mRoomId).
                    extra(Constants.MEETING_SENDER, mMeetingSender).start();
            mCallType = -1;
        }

        TrafficService_.intent(this).start();
    }

    @Override
    protected void onResume() {
        super.onResume();
        String uid = EApplication.getSharedPreferences().getString(Constants.LOGIN_UID, "");
        if (!TextUtils.isEmpty(uid)) {
            mTvName.setText(uid);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EApplication.mInitState = false;
    }

    @Click(R.id.layout_live)
    void onLive() {
        transitionTo(LiveShowActivity_.intent(this).get());
    }

    @Click(R.id.layout_meeting)
    void onMeeting() {
        transitionTo(MeetingActivity_.intent(this).get());
    }

    @Click(R.id.layout_call)
    void onCall() {
        transitionTo(VideoCallActivity_.intent(this).get());
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (isLoading()) {
                dismissLoading();
            } else {
                mAccountHelper.logout();
                //退出程序后把事件给service, service唤醒后重新获取事件
                TrafficService_.intent(this).start();
                finishPage();
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @OnActivityResult(LOGIN_REQUEST)
    void onLoginResult(int result) {
        if (result != RESULT_OK) finishPage();
    }
}
